package com.dongdongshop.serviec.brand;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.seller.TbGoodsDescMapper;
import com.dongdongshop.mapper.seller.TbGoodsMapper;
import com.dongdongshop.mapper.seller.TbItemMapper;
import com.dongdongshop.pojo.seller.TbGoods;
import com.dongdongshop.pojo.seller.TbGoodsDesc;
import com.dongdongshop.pojo.seller.TbItem;
import com.dongdongshop.service.GoodsService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Stream;

@Component
@Service
public class GoodsServiceImpl implements GoodsService {
    @Resource
    private TbGoodsMapper tbGoodsMapper;
    @Resource
    private TbGoodsDescMapper tbGoodsDescMapper;
    @Resource
    private TbItemMapper itemMapper;

    @Override
    public TbGoods getGoodsById(Long goodsId) {
        return tbGoodsMapper.selectByPrimaryKey(goodsId);
    }

    @Override
    public List<TbGoods> listGoodsBySellerId(String sellerId) {
        return tbGoodsMapper.listGoodsBySellerId(sellerId);
    }

    @Override
    public int updateStatus(Long[] ids,String flag) {
        return tbGoodsMapper.updateStatus(ids,flag);
    }



    @Override
    public int addGood(TbGoods goods, TbGoodsDesc desc, List<TbItem> list, String sellerName) {
        try {
            System.out.println("进入方法"+list.size());
            tbGoodsMapper.addById(goods);//返回id
            desc.setGoodsId(goods.getId()); //增加goods_desc表
            tbGoodsDescMapper.addGoodsDesc(desc);
            //增加item表
            for (TbItem tbItem : list) {
                System.out.println("item"+tbItem);
                tbItem.setCreateTime(new Date());
                tbItem.setUpdateTime(new Date());
                tbItem.setGoodsId(goods.getId());
                tbItem.setSellerId(goods.getSellerId());
                tbItem.setSeller(sellerName);
                itemMapper.insertSelective(tbItem);
            }
            return 1;
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }

    }
}
